<template>
  <div @keyup.enter="getData()">
    <el-form class="query-table-form" :inline="true">
      <el-form-item>
        <el-input v-model="params.batch_no" placeholder="批次号查询" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="params.bank_name" placeholder="银行名称查询" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" @click="getData()">查询</el-button>
      </el-form-item>
    </el-form>

    <c-table border v-loading="loading" :columns="tableColumns" :data="tableData">
      <template v-slot:ab_name="scope">
        <div>{{ `A角：${scope.row.res_a_name || '-'}` }}</div>
        <div>{{ `B角：${scope.row.res_b_name || '-'}` }}</div>
      </template>
    </c-table>

    <div class="query-pagination">
      <div></div>
      <el-pagination
        layout="total, prev, pager, next, jumper"
        :current-page.sync="params.index"
        :page-size="params.size"
        :total="totalCount"
        @current-change="getData"
      ></el-pagination>
    </div>

    <recommend-file-dialog
      :visible.sync="attachVisible"
      :batch-data="attachBatchData"
      view-mode
    ></recommend-file-dialog>

    <approve-dialog
      :visible.sync="editVisible"
      :query-id="editQueryId"
      @success="getData(params.index)"
    ></approve-dialog>

    <el-dialog
      :visible.sync="viewVisible"
      :close-on-click-modal="false"
      append-to-body
      top="30px"
      width="1000px"
      title="查看"
      class="dialog-sm"
    >
      <el-tabs>
        <el-tab-pane label="项目审批表">
          <approve-view :query-id="viewQueryId" view-mode></approve-view>
        </el-tab-pane>
        <el-tab-pane label="项目明细">
          <submit-list :query-id="viewQueryId" view-mode></submit-list>
        </el-tab-pane>
      </el-tabs>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import {
  approveBaseColumns,
  approveDataAdapter,
  getInvingPerson,
  getFollow,
} from '@/credit/views/county/unionFast/invest/components/index'

import SubmitList from '@/bank/views/fast/recommend/SubmitList'
import RecommendFileDialog from '@/bank/views/fast/recommend/RecommendFileDialog'
import ApproveDialog from '@/credit/views/county/unionFast/invest/components/MainDialog'
import ApproveView from '@/credit/views/county/unionFast/invest/components/Main'

export default {
  components: {
    SubmitList,
    RecommendFileDialog,
    ApproveDialog,
    ApproveView,
  },
  props: {},
  data() {
    return {
      params: {
        index: 1,
        size: 15,
        batch_no: '',
        bank_name: '',
        'claim_status.eq': 1,
        'item_status.eq': 1,
        'approval_status.eq': 1,
        'confuse_status.neq': 3,
      },
      order: {
        claim_time: 'desc',
      },

      loading: false,
      exportLoading: false,
      tableData: [],
      totalCount: 0,
      tableColumns: [
        ...approveBaseColumns,
        { attrs: { label: '待处理人员', prop: 'itemStatusLabel', width: 80, align: 'center' } },
        {
          attrs: { label: '操作', align: 'center', width: 170 },
          operations: [
            {
              content: '项目审批',
              icon: 'fa fa-gavel',
              event: this.handleApprove,
              ifRender: it => it.editEnable,
            },
            { content: '附件（推荐函）', icon: 'fa fa-clipboard', event: this.handleAttach },
            { content: '查看', icon: 'fa fa-search', event: this.handleView },
          ],
        },
      ],

      // 查看
      viewVisible: false,
      viewQueryId: '',

      // 附件
      attachVisible: false,
      attachBatchData: {},

      // 审批
      editVisible: false,
      editQueryId: '',
    }
  },
  computed: {
    ...mapGetters(['clientId', 'userId']),
  },
  watch: {},
  created() {
    this.params['service_id.eq'] = this.clientId
    this.getData()
  },
  mounted() {},
  methods: {
    getData(index = 1) {
      this.getIngData(index)
    },

    getIngData(index) {
      this.params.index = index
      this.loading = true
      this.$api
        .getData({
          table: 'customer_related_quick_batch',
          params: this.params,
          order: this.order,
        })
        .then(res => {
          const data = res.data || {}
          const list = data.data || []
          this.totalCount = data.totalCount || 0
          return Promise.all([getInvingPerson(list), getFollow(list)]).then(([person, follow]) => {
            return { list, person, follow }
          })
        })
        .then(({ list, person, follow }) => {
          this.tableData = list.map(item => {
            const relatedId = item.customer_related_quick_batch_id
            item = approveDataAdapter(item)

            // 可编辑状态
            const conData = person[relatedId] || []
            item.editEnable = conData.some(it => it.task_person === this.userId)

            // 当前人
            const data = follow.find(it => it.wf_related_id === relatedId) || {}
            item.itemStatusLabel = data.innerFollow || '-'

            return item
          })
        })
        .finally(() => {
          this.loading = false
        })
    },

    handleView(row = {}) {
      this.viewQueryId = row.customer_related_quick_batch_id
      this.viewVisible = true
    },

    handleAttach(row = {}) {
      this.attachBatchData = { ...row }
      this.attachVisible = true
    },

    handleApprove(row) {
      this.editQueryId = row.customer_related_quick_batch_id
      this.editVisible = true
    },
  },
}
</script>

<style lang="scss" scoped></style>
